package com.huitian.mobileshop;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class Utils {
	//判断网络是否可用
	public static boolean isNetworkEnable(Context context){
		ConnectivityManager manager=(ConnectivityManager)context
				.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
		if(manager== null) return false;
		NetworkInfo info=manager.getActiveNetworkInfo();
		if(info==null||!info.isAvailable())
			return false;
		return true;
	}
	//初始化webview的相关属性
	public static void initWebViewSettings(WebView webView,Context context){
		webView.getSettings().setJavaScriptEnabled(true);
		webView.setWebViewClient(new  MyWebClient(context));
		final Context ctx=context;
		webView.setWebChromeClient(new WebChromeClient(){
			@Override
			public boolean onJsConfirm(WebView view, String url,
					String message, JsResult result) {
				// TODO Auto-generated method stub
				  Builder builder = new Builder(ctx);
				  final JsResult jr =result;
				  
                  builder.setTitle("确认");
                  builder.setMessage(message);
                  builder.setPositiveButton("是", new AlertDialog.OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                    	  jr.confirm();
                          
                      }
  
                  });
                  builder.setNegativeButton("否", new AlertDialog.OnClickListener() {
  
                   public void onClick(DialogInterface dialog, int which) {
                          jr.cancel();
                      }
  
                  });
                  builder.setCancelable(false);
                  builder.create();
                  builder.show();
                  return true;
			}
		});
		webView.setHorizontalScrollBarEnabled(false);
		webView.setVerticalScrollBarEnabled(false);
		webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
	}
	public static void initWeb(String url,Context context,WebView webView){
		if(!Utils.isNetworkEnable(context)){
			Toast.makeText(context, "当前无网络连接", Toast.LENGTH_SHORT).show();
			url="file:///android_asset/networkfailed.html";
		}
		webView.loadUrl(url);
	}
	
}
